<?php
##################
# INSTRUCTIONS
#
# 1) Check if you has PHP installed as **CGI** or as an **Apache-Mod**.
# Create a PHP file with a single line : phpinfo();
# Load that file in a browser, and check if PHP uses CGI or an Apache Module
#
# 2) If you have PHP as CGI
# Uncomment (remove the trailing #) the following line :
# !/usr/local/bin/php -q
# Change the path to your "php" executable, if needed
# Create a crontab : crontab -e (it will open vi editor)
# OR Edit /etc/crontab and add the following line (with all the start, without the # ; see below 4):
# * * * * * php /path/to/your/cron.php
# Change script permissions to 755:
# sudo chmod 755 /path/to/your/cron.php
# Run the following command in the console:
# crontab crontab
#
# 3) If you have PHP as an Apache-Mod
# Install Lynx or wget or curl if you don't already have them (use curl on Mac)
# Create a crontab : crontab -e (if will open vi editor)
# OR Edit /etc/crontab and add one of those lines (see below, 4):
# * * * * * lynx -dump http://www.yoursite.com/path/to/script.php
# * * * * * /usr/bin/curl -0 http://www.yoursite.com/path/to/script.php
# * * * * * /usr/bin/wget -q -O /dev/null http://www.yoursite.com/path/to/script.php
# Refresh the cronjobs with this command:
# crontab crontab
#
# 4) How to configure the cron job ?
# See: http://en.wikipedia.org/wiki/Cron
# 
# *    *    *    *    *      command to be executed
# -    -    -    -    -
# |    |    |    |    |
# |    |    |    |    +----- day of week (0 - 6) (Sunday=0)
# |    |    |    +---------- month (1 - 12)
# |    |    +--------------- day of month (1 - 31)
# |    +-------------------- hour (0 - 23)
# +------------------------- min (0 - 59)
#
# Some exemples:
# * * * * * * php myscript.php #every minute
# * 23 * * * php myscript.php #at 23pm, every day
# * 3,6,9,12 * * * php myscript.php #at 3am, 6am, 9am and 12, every day
# * 8-20 * * * php myscript.php #every hour between 8am and 20pm, every day
# 5 * * * * php myscript.php #every 5 minutes
#
# Useful commands:
# - crontab -e : create a new cron job and open vi editor
# - crontab -l : lists all cron jobs
# - crontab -r : remove a cron job
#
#############

############# 
# Call core libraries and some magic
header('Content-type: text/html; charset=UTF-8'); 
include('../functions.inc.php');


#############
# Load all users feeds
$prefs = Preferences::getAllUsersValues('rss_feed_url');
foreach($prefs as $k=>$values)
	foreach($values as $url) // loop every url
	{
		#############
		# Do the job
		$xp = new XMLParser($url);
		$res = $xp->storeArticles();
		#############
		# Display the results
		$now = date("d/m/Y H:i:s");
		$count = 0;
		$result = "";
		foreach($res as $title)
		{
			$result .= "<li>$title</li>";
			$count++;
		}
		echo "<p><b>Updating feed [$url] at $now</b></p><p><ol>$result</ol></p><p><b>$count items processed.</b></p><hr />";
	}
?>